package com.rub.android.jetpack

import android.os.Bundle
import android.view.ContextMenu
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import com.rub.android.R
import com.rub.android.utils.logI

class NavigateActivity : AppCompatActivity() {

    lateinit var navController: NavController
    lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigate)
        navController = Navigation.findNavController(this, R.id.fragmentContainerView)

        appBarConfiguration = AppBarConfiguration.Builder(navController.graph).build()

        //此行代码，主题不能设置为NoActionBar
        NavigationUI.setupActionBarWithNavController(this, navController,appBarConfiguration)

        //监听页面切换
        navController.addOnDestinationChangedListener { controller, destination, arguments ->

            logI(TAG,"addOnDestinationChangedListener")
        }
    }

    //支持返回键操作
    override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(navController,appBarConfiguration)||navController.navigateUp()
    }

    //菜单设置
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)
        //设置菜单
        menuInflater.inflate(R.menu.menu_detail, menu)
        return true
    }

    //选择菜单
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return NavigationUI.onNavDestinationSelected(item,navController)|| super.onOptionsItemSelected(item)
    }

    companion object {
        private val TAG = javaClass.simpleName
    }

}